<?php
/**
 * AutoLoad.class.php
 * 
 * PDK自动加载引导类
 * 
 * @author lsl <470663151@qq.com> 
 * @copyright Copyright (c) 2012 lsl8966 Software Studio
 * @link http://www.cnblogs.com/lsl8966
 * @version $Id: KindeditorModule.class.php 1.0 2012-01-18 22:48:01Z lsl8966 $
 * @package core
 * @since 1.0
 */

if (!defined('IN_PDK')){
    exit();
}

class KindeditorModule extends Module{
    /**
     * kindeditor js文件加载次数
     * 
     * @var integer 
     */
     public static $load_nums = 0;
    
    /**
     * 构造函数
     * 
     * @access public 
     * @return void 
     */
     public function __construct(){
        
         }
    
    /**
     * 获取编辑器
     * 
     * @access public 
     * @param string $editor_id textarea标签的ID
     * @param string $browse_url 浏览服务器图片文件页面的URL.默认为相关页面(高级应用)
     * @param string $upload_url 图片上传服务页面的URL.默认为相关页面(高级应用)
     * @return string 
     */
     public function get_editor($editor_id, $browse_url = null, $upload_url = null){
        
         // define current module url
        $base_url = SITE_PATH . 'modules/kindeditor/';
        
         $output_str = '';
         if (self :: $load_nums == 0){
             $output_str .= '<script type="text/javascript" src="' . $base_url . 'kindeditor\kindeditor-min.js"></script>';
             }
        
         $ext_str = '';
         $ext_str .= 'fileManagerJson:\'' . ((!is_null($browse_url)) ? $browse_url : $base_url . 'kindeditor\php\file_manager_json.php') . '\',';
         $ext_str .= 'imageUploadJson:\'' . ((!is_null($upload_url)) ? $upload_url : $base_url . 'kindeditor\php\upload_json.php') . '\',';
        
         $output_str .= "<script type=\"text/javascript\">KE.show({id:'{$editor_id}',{$ext_str} allowFileManager:true,});</script>";
        
         self :: $load_nums ++;
         setcookie('pdk_kindeditor_auth', md5('pdk'), time() + 3600, '/');
        
         return $output_str;
         }
    
    /**
     * 获取简单主题的编辑器
     * 
     * @access public 
     * @param string $editor_id textarea标签的ID
     * @return string 
     */
     public function get_simple_editor($editor_id){
        
         // define current module url
         $base_url = SITE_PATH . 'modules/kindeditor/';

         $output_str = '';
         if (self :: $load_nums == 0){
             $output_str .= '
            <script charset="utf-8" src="' . $base_url . 'kindeditor/kindeditor.js?111"></script>
            <script charset="utf-8" src="' . $base_url . 'kindeditor/lang/zh_CN.js"></script>
            <script charset="utf-8" src="' . $base_url . 'kindeditor/plugins/code/prettify.js"></script>';
         }
         $output_str .= "<script>
                var editor_".$editor_id.";
				KindEditor.ready(function(K) {
					    editor_".$editor_id." = K.create('textarea[name=" . $editor_id . "]', {
						cssPath : '" . $base_url . "kindeditor/plugins/code/prettify.css',
						uploadJson : '" . $base_url . "kindeditor/php/upload_json.php',
						fileManagerJson : '" . $base_url . "kindeditor/php/file_manager_json.php',
						allowFileManager : true,
						width:'100%',
						items : ['source',
						'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
						'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
						'insertunorderedlist', '|', 'emoticons', 'image', 'link'],
						afterCreate : function() {
							var self = this;
							K.ctrl(document, 13, function() {
								self.sync();
								K('form[name=example]')[0].submit();
							});
							K.ctrl(self.edit.doc, 13, function() {
								self.sync();
								K('form[name=example]')[0].submit();
							});   
						},afterBlur: function(){
						    this.sync();
						}
					});	
					prettyPrint();
				});
			</script>
		";
         return $output_str;
     }
    
    /**
     * 获取简单主题的编辑器
     *
     * @access public
     * @param string $editor_id textarea标签的ID
     * @return string
     */
     public function get_all_editor($editor_id){

         // define current module url
        $base_url = SITE_PATH . 'modules/kindeditor/';

         $output_str = '';
         if (self :: $load_nums == 0){
             $output_str .= '
            <script charset="utf-8" src="' . $base_url . 'kindeditor/kindeditor.js"></script>
            <script charset="utf-8" src="' . $base_url . 'kindeditor/lang/zh_CN.js"></script>
            <script charset="utf-8" src="' . $base_url . 'kindeditor/plugins/code/prettify.js"></script>';
         }
         $output_str .= "<script>
                var editor_".$editor_id.";
				KindEditor.ready(function(K) {
					    editor_".$editor_id." = K.create('textarea[name=" . $editor_id . "]', {
						cssPath : '" . $base_url . "kindeditor/plugins/code/prettify.css',
						uploadJson : '" . $base_url . "kindeditor/php/upload_json.php',
						fileManagerJson : '" . $base_url . "kindeditor/php/file_manager_json.php',
						allowFileManager : true,
						afterCreate : function() {
							var self = this;
							K.ctrl(document, 13, function() {
								self.sync();
								K('form[name=example]')[0].submit();
							});
							K.ctrl(self.edit.doc, 13, function() {
								self.sync();
								K('form[name=example]')[0].submit();
							});
						},afterBlur: function(){
						    this.sync();
						}
					});
					prettyPrint();
				});
			</script>
		";
         return $output_str;
     }
    
    /**
     * 析构函数
     * 
     * @access public 
     * @return void 
     */
     public function __destruct(){
         self :: $load_nums = 0;
     }
}
